package com.mopa.qzonegallery;
import android.content.Context;
import android.graphics.Bitmap;
import android.view.Gravity;
import android.view.View;
import android.widget.Button;
import android.widget.FrameLayout;
import android.widget.ImageView;
import android.widget.RelativeLayout;
public class ImageItemLayout extends RelativeLayout{
    private static final int ID_IMG_PIC = 11;
	public  ImageView mImgPic;
    private Button mImgStatus;
    public  ImageItemInfo curItem;
	public ImageItemLayout(Context context) {
		super(context);
		initLayout(context);
		// TODO Auto-generated constructor stub
	}
	private void initLayout(Context context){
	   //RLayout;
		RelativeLayout.LayoutParams relativeParams=new RelativeLayout.LayoutParams(PhoneUtils.getRealPixelW(222),LayoutParams.WRAP_CONTENT);
		RelativeLayout Rlayout=new RelativeLayout(context);
		Rlayout.setBackgroundColor(0xffffffff);
		Rlayout.setPadding(PhoneUtils.getRealPixelW(6), PhoneUtils.getRealPixelW(6), PhoneUtils.getRealPixelW(6), 0);
		this.addView(Rlayout,relativeParams);
		
		relativeParams=new RelativeLayout.LayoutParams(LayoutParams.FILL_PARENT,PhoneUtils.getRealPixelW(170));
		mImgPic=new ImageView(context);
		mImgPic.setId(ID_IMG_PIC);
		Rlayout.addView(mImgPic, relativeParams);
		
		
		relativeParams=new RelativeLayout.LayoutParams(LayoutParams.FILL_PARENT,PhoneUtils.getRealPixelW(50));
		relativeParams.addRule(RelativeLayout.BELOW,ID_IMG_PIC);
		FrameLayout statusLayout=new FrameLayout(context);
		statusLayout.setPadding(0, PhoneUtils.getRealPixelW(2), 0, PhoneUtils.getRealPixelW(2));
		Rlayout.addView(statusLayout,relativeParams);
		
		//mImgStatus
		FrameLayout.LayoutParams frameParams=new FrameLayout.LayoutParams(LayoutParams.WRAP_CONTENT,LayoutParams.WRAP_CONTENT);
		frameParams.gravity=Gravity.CENTER;
		mImgStatus=new Button(context);
		mImgStatus.setBackgroundDrawable(null);
		mImgStatus.setText("UpLoad");
		mImgStatus.setTextSize(20.f);
		mImgStatus.setTextColor(0xffFB0303);
		mImgStatus.getPaint().setFakeBoldText(true);
		statusLayout.addView(mImgStatus, frameParams);
		
	}

	public void initItem(ImageItemInfo item) {
		// TODO Auto-generated method stub
		curItem=item;
		setPostStatus();
		Bitmap bm=ImageCache.getImageCacheImg(curItem.getImageThumbnailPath());
		if(bm!=null)
		{
			mImgPic.setImageBitmap(bm);
		}
		else
		{
			mImgPic.setImageBitmap(null);
		    ImageCache.getImageQueueInstance().addItem(new AsyncImageInfo(curItem.getImageThumbnailPath()));
			ImageCache.getAsynImageLoaderInstance().loadImage(AsyncImageLoader.MSG_RUN_SDCARD_M);
		}
		mImgStatus.setOnClickListener(new OnClickListener() {
			
			@Override
			public void onClick(View v) {
				// TODO Auto-generated method stub
				if(curItem.getUpStatus()==UpStatus.UnPost)
				{
					PostTask.addTask(curItem.getImageThumbnailPath(),UpStatus.OnlyChoose);
					curItem.setUpStatus(UpStatus.PostWait);
				}
				else if(curItem.getUpStatus()==UpStatus.PostWait)
				{
					PostTask.removeTask(curItem.getImageThumbnailPath(),UpStatus.OnlyChoose);
					curItem.setUpStatus(UpStatus.UnPost);
				}
				setPostStatus();
			}
		});
	}
	public void setPostStatus()
	{
		int status=curItem.getUpStatus();
		switch (status) {
		case UpStatus.UnPost:
			mImgStatus.setText("UnChoose");
			break;
		case UpStatus.PostWait:
			mImgStatus.setText("Choosed");
			break;
		case UpStatus.Posting:
			mImgStatus.setText("Posting");
			break;
		case UpStatus.Posted:
			mImgStatus.setText("Posted Success");
			break;
		case UpStatus.PostFailed:
			mImgStatus.setText("Posted Fail");
			break;
		default:
			break;
		}
	}
}
